<cfparam name="Volunteer_ID" type="integer" default="0" />
<cfparam name="Organization_ID" type="integer" default="0" />
<cfparam name="returnView" type="string" default="volunteers" />
<cfparam name="passback" type="string" default="" />

<cfscript>
	volunteerGateway = new assets.calgrants.com.gateway.VolunteerGateway();
	volunteer = volunteerGateway.get(Volunteer_ID = Volunteer_ID);
	if (volunteer.getVolunteer_ID() eq 0 AND request.authUser.organizationID gt 0) {
		volunteer.setOrganization_ID(request.authUser.organizationID);
	} else if (Organization_ID gt 0) {
		// this is important!  this allows a pool volunteer with an existing id to be claimed by an organization
		// by duplicating the record for the organization.  by allowing a volunteer to be first selected, but 
		// then setting the volunteer_id to 0, the volunteer will be copied to the selected organization
		volunteer.setVolunteer_ID(0);
		volunteer.setOrganization_ID(Organization_ID);
	}
</cfscript>

<cfif volunteer.getOrganization_ID() eq 0>
	
	<cfset returnView = "editVolunteer" />
	<cfset cancelView = "volunteers" />
	<cfset passback = "Volunteer_Id,#Volunteer_ID#" />
	<cfinclude template="/assets/calgrants/ui/forms/selectOrganization.cfm">

<cfelseif IsNumeric(volunteer.getVolunteer_ID())>

	<cfscript>
		base = new assets.calgrants.com.Base();
		passbackQueryString = base.passbackToQueryString(passback);
		organizationGateway = new assets.calgrants.com.gateway.OrganizationGateway();
		organization = organizationGateway.get(Organization_ID = volunteer.getOrganization_ID());
		//organizations = organizationGateway.query(regionIDs = IIf(IsUserInRole("Regional Admin"), request.authUser.regionID, ""), Organization_ID = IIf(IsUserInRole("Organizer") OR IsUserInRole("Site Contact"), request.authUser.organizationID, ""));
		countyGateway = new assets.calgrants.com.gateway.CountyGateway();
		counties = countyGateway.query();
		workshopGateway = new assets.calgrants.com.gateway.WorkshopGateway();
		workshops = workshopGateway.query(Organization_ID = volunteer.getOrganization_ID(), Program_Year = request.currentProgramYear);
		//workshopVolunteerGateway = new assets.calgrants.com.gateway.WorkshopVolunteerGateway();
		//volunteerWorkshops = workshopVolunteerGateway.query(Volunteer_ID = volunteer.getVolunteer_ID());
		//workshopVolunteerIDs = ValueList(volunteerWorkshops.Workshop_Volunteer_ID);
		//workshopIDs = ValueList(volunteerWorkshops.Workshop_ID);
		//statuses = ValueList(volunteerWorkshops.Status);
	</cfscript>
	
	<script type="text/javascript" language="javascript">
	<!--
		<cfoutput>
			var volunteerID = #volunteer.getVolunteer_ID()#;
			var organizationID = #volunteer.getOrganization_ID()#;
			var countyID = #volunteer.getCounty_ID()#;
			
			<!--- //var organizations =  new kendo.data.DataSource();
			//organizations.data(#SerializeJSON(base.QueryToStruct(organizations), false)#);
			//organizations.insert(0, {organization: "Select...", address: "Select to make a member of the", city: "", zip_code: "", organization_id: 0}); --->
			
			var counties =  new kendo.data.DataSource();
			counties.data(#SerializeJSON(base.QueryToStruct(counties), false)#);
			if (volunteerID == 0) {
				counties.insert(0, {county_id: "", county: "Select...", region_id: "", region: ""});
			}
		</cfoutput>
		
		toggle = function(sel) {
			if (sel == "Translator") {
				if ($("#Translator").is(":checked")) {
					$("#translatorLanguagesContainer").show();
				} else {
					$("#translatorLanguagesContainer").hide();
					$("#Translator_Languages").val("");
				}
			}
		};
		
		validate = function() {
			isValid = true;
			errorMessages = [];
			if ($("#Translator").is(":checked")) {
				if ($("#Translator_Languages").val().length == 0) {
					isValid = false;
					errorMessages.push("List the translator languages");
				}
			}
			if (errorMessages.length > 0) {
				var errorMessage = "";
				for (var i=0; i<errorMessages.length; i++) {
					errorMessage += errorMessages[i] + "\n";	
				}
				alert(errorMessage);
			}
			return isValid;				
		}
		
		$(function() {
			$("#volunteerModal").modal();
			resizeModal("volunteerModal");
			
			/*organizationDropdown = $("#Organization_ID").kendoDropDownList({
				dataTextField: "organization",
				dataValueField: "organization_id",
				dataSource: organizations,
				template: 	'<strong>${organization}</strong><br>' +
							'${address}' +
							' # if (data.city.length > 0) { # ' +
							'<br>${city}, CA ${zip_code}' +
							' # } # ',
				change: function() {
					//refreshOrganization();
				}
			}).data("kendoDropDownList");
			if (volunteerID == 0) {
				organizationDropdown.select(0);
			} else {
				organizationDropdown.value(organizationID);
			}*/
			
			countyDropdown = $("#County_ID").kendoDropDownList({
				dataTextField: "county",
				dataValueField: "county_id",
				dataSource: counties,
				template: 	'<strong>${county}</strong><br>' +
							' # if (region_id != "") { # ' +
							'${region_id} - ${region} Region' +
							' # } # '
			}).data("kendoDropDownList");
			if (countyID == 0) {
				countyDropdown.select(0);
			} else {
				countyDropdown.value(countyID);
			}
			
			travelDistanceDropdown = $("#Travel_Distance").kendoDropDownList().data("kendoDropDownList");
			shirtSizeDropdown = $("#Shirt_Size").kendoDropDownList().data("kendoDropDownList");
			
			if (!$("#Translator").is(":checked")) {
				$("#translatorLanguagesContainer").hide();
			}
		})
	//-->
	</script>
	
	<cfoutput>
	
		<cfform id="volunteerForm" name="volunteerForm" action="#script_name#" method="post" onsubmit="return validate();">
			<cfinput type="hidden" name="action" value="saveVolunteer" />
			<cfinput type="hidden" name="view" value="#returnView#" />
			<cfinput type="hidden" name="Volunteer_ID" value="#volunteer.getVolunteer_ID()#" />
			<cfinput type="hidden" name="Organization_ID" value="#volunteer.getOrganization_ID()#" />
			<cfif IsDefined("passback")>
				<cfloop index="keyValue" list="#passback#" delimiters=";">
					<cfinput type="hidden" name="#ListFirst(keyValue)#" value="#ListLast(keyValue)#" />
				</cfloop>
			</cfif>
			<div id="volunteerModal" class="modal modal-fullscreen" data-backdrop="static">
				<div class="modal-header">
					<h3>Volunteer</h3>
				</div>
				<div class="modal-body">
					<div class="container-fluid">
						<div class="row-fluid">
							<div class="span4">
								<div class="clearfix required">
									<label class="required">First Name</label>
									<cfinput type="text" name="First_Name" value="#volunteer.getFirst_Name()#" required="yes" message="First Name is required" maxlength="100" class="span9">
								</div>
								<div class="clearfix required">
									<label class="required">Last Name</label>
									<cfinput type="text" name="Last_Name" value="#volunteer.getLast_Name()#" required="yes" message="Last Name is required" maxlength="100" class="span9">
								</div>
								<div class="clearfix required">
									<label class="required">Address</label>
									<cfinput type="text" name="Address" value="#volunteer.getAddress()#" required="yes" message="Address is required" maxlength="200" class="span9">
								</div>
								<div class="clearfix required">
									<label class="required">City</label>
									<cfinput type="text" name="City" value="#volunteer.getCity()#" required="yes" message="City is required" maxlength="100" class="span9">
								</div>
								<div class="clearfix required">
									<label class="required">Zip Code</label>
									<cfinput type="text" name="Zip_Code" value="#volunteer.getZip_Code()#" required="yes" validate="zipcode" message="Zip Code is required and must be a valid zip code" maxlength="5" placeholder="##########" class="span9">
								</div>
								<div class="kendo-control clearfix">
									<label class="required">County</label>
									<cfselect name="County_ID" required="yes" message="County is required" class="span9" style="margin: 0;"></cfselect>
								</div>
								<div class="clearfix required">
									<label class="required">E-mail</label>
									<cfinput type="text" name="Email" value="#volunteer.getEmail()#" required="yes" message="E-mail is required and must be a valid e-mail address" validate="email" maxlength="100" class="span9">
								</div>
								<div class="clearfix required">
									<label>Phone</label>
									<cfinput type="text" name="Phone" value="#volunteer.getPhone()#" required="no" validate="telephone" message="Phone must be a valid telephone number" maxlength="15" placeholder="######-######-########" class="span6"> <cfinput type="text" name="Extension" value="#volunteer.getExtension()#" required="no" maxlength="10" placeholder="Extension" class="span3">
								</div>
							</div>
							<div class="span4">
								<!--- <div class="clearfix">
									<label>Organization/Company</label>
									<cfinput type="text" name="Organization" value="#volunteer.getOrganization()#" required="no" maxlength="100" class="span9">
								</div>
								<div class="clearfix">
									<label>Title</label>
									<cfinput type="text" name="Title" value="#volunteer.getTitle()#" required="no" maxlength="100" class="span9">
								</div> --->
								
								<div class="text-control clearfix">
									<label>Specialties/Expertise</label>
							
									<div class="well well-framed clearfix" style="line-height: 24px;">
										<label class="checkbox">
											<input type="checkbox" name="Student" value="1"<cfif volunteer.getStudent()> checked</cfif>>
											Student
										</label>
										<label class="checkbox">
											<input type="checkbox" name="Financial_Aid_Expert" value="1"<cfif volunteer.getFinancial_Aid_Expert()> checked</cfif>>
											Financial aid expert
										</label>
										<label class="checkbox">
											<input type="checkbox" name="FAFSA_Expert" value="1"<cfif volunteer.getFAFSA_Expert()> checked</cfif>>
											FAFSA expert
										</label>
										<label class="checkbox">
											<input type="checkbox" name="AB540_Expert" value="1"<cfif volunteer.getAB540_Expert()> checked</cfif>>
											CA Dream Act/AB540 expert
										</label>
										<label class="checkbox">
											<input type="checkbox" name="Finance_Expert" value="1"<cfif volunteer.getFinance_Expert()> checked</cfif>>
											Finance expert
										</label>
										<label class="checkbox">
											<input type="checkbox" name="Tax_Expert" value="1"<cfif volunteer.getTax_Expert()> checked</cfif>>
											Tax expert
										</label>
										<label class="checkbox">
											<input type="checkbox" name="Outreach_Expert" value="1"<cfif volunteer.getOutreach_Expert()> checked</cfif>>
											Outreach expert
										</label>
										<label class="checkbox">
											<input type="checkbox" name="Technology_Expert" value="1"<cfif volunteer.getTechnology_Expert()> checked</cfif>>
											Technology expert
										</label>
										<label class="checkbox">
											<input type="checkbox" id="Translator" name="Translator" value="1" onclick="toggle('Translator')"<cfif volunteer.getTranslator()> checked</cfif>>
											Translator
										</label>
										<!--- <div id="translatorLanguagesContainer">
											<cfinput type="text" id="Translator_Languages" name="Translator_Languages" value="#volunteer.getTranslator_Languages()#" class="span9" />
										</div> --->
									</div>
								</div>
								<div class="kendo-control clearfix">
									<label class="required">Travel Distance</label>
									<cfselect name="Travel_Distance" required="yes" message="Travel Distance is required" class="span9" style="margin: 0;">
										<cfif volunteer.getVolunteer_ID() eq 0>
											<option value="" selected>Select...</option>
										</cfif>
										<option value="10"<cfif volunteer.getTravel_Distance() eq "10"> selected</cfif>>Up to 10 miles</option>
										<option value="20"<cfif volunteer.getTravel_Distance() eq "20"> selected</cfif>>Up to 20 miles</option>
										<option value="50"<cfif volunteer.getTravel_Distance() eq "50"> selected</cfif>>Up to 50 miles</option>
										<option value="100"<cfif volunteer.getTravel_Distance() eq "100"> selected</cfif>>Up to 100 miles</option>
										<option value="200"<cfif volunteer.getTravel_Distance() eq "200"> selected</cfif>>Up to 200 miles</option>
										<option value="0"<cfif volunteer.getTravel_Distance() eq "0" and volunteer.getVolunteer_ID() gt 0> selected</cfif>>Any distance (statewide)</option>
									</cfselect>
								</div>
								<div class="kendo-control clearfix">
									<label class="required">Shirt Size</label>
									<cfselect name="Shirt_Size" required="yes" message="Shirt Size is required" class="span9" style="margin: 0;">
										<cfif volunteer.getVolunteer_ID() eq 0>
											<option value="" selected>Select...</option>
										</cfif>
										<!--- <option value="S"<cfif volunteer.getShirt_Size() eq "S"> selected</cfif>>Small</option> --->
										<option value="M"<cfif volunteer.getShirt_Size() eq "M"> selected</cfif>>Medium</option>
										<option value="L"<cfif volunteer.getShirt_Size() eq "L"> selected</cfif>>Large</option>
										<option value="XL"<cfif volunteer.getShirt_Size() eq "XL"> selected</cfif>>X-Large</option>
										<option value="XXL"<cfif volunteer.getShirt_Size() eq "XXL"> selected</cfif>>XX-Large</option>
									</cfselect>
								</div>
							</div>
							<div class="span4">
								<!--- <div class="text-control clearfix">
									<label>Organization</label>
									#organization.getOrganization()#
								</div>
								<div class="kendo-control clearfix">
									<label class="required">Organization</label>
									<cfselect name="Organization_ID" required="yes" message="Organization is required" class="span9" style="margin: 0;"></cfselect>
								</div> --->
								<p><a href="#script_name#?view=editVolunteer&Volunteer_ID=0" class="btn btn-success">Assign to workshop</a></p>
								<!--- only allow assignment ot approved workshops --->
								<label>Workshops</label>
								<cfif workshops.RecordCount gt 0>						
									<table class="table table-striped table-bordered">
										<thead>
											<tr>
												<th>Status</th>
												<th>Site/Date/Time</th>
											</tr>
										</thead>
										<tbody>
											<cfloop query="workshops">
												<cfset idx = ListFind(workshopIDs, Workshop_ID) />
												<cfif idx gt 0>
													<cfinput type="hidden" name="Workshop_Volunteer_IDs" value="#ListGetAt(workshopVolunteerIDs, idx)#" />
													<cfset status = ListGetAt(statuses, idx) />
												<cfelse>
													<cfinput type="hidden" name="Workshop_Volunteer_IDs" value="0" />
													<cfset status = "Unassigned" />
												</cfif>
												<cfinput type="hidden" name="Workshop_IDs" value="#Workshop_ID#" />
												<tr>
													<td>
														<cfselect name="Workshop_Statuses" style="width: 135px;">
															<option value="Unassigned"<cfif status eq "Unassigned"> selected</cfif>>Unassigned</option>
															<option value="Assigned"<cfif status eq "Assigned"> selected</cfif>>Assigned</option>
															<option value="Confirmed"<cfif status eq "Confirmed"> selected</cfif>>Confirmed</option>
															<option value="Attended"<cfif status eq "Attended"> selected</cfif>>Attended</option>
															<option value="Acknowledged"<cfif status eq "Acknowledged"> selected</cfif>>Acknowledged</option>
														</cfselect>
													</td>
													<td>
														#Site#<br />
														#DateFormat(Workshop_Date, "m/d/yyyy")# #TimeFormat(Start_Time, "h:mmtt")#&nbsp;-&nbsp;#TimeFormat(End_Time, "h:mmtt")#
													</td>
												</tr>
											</cfloop>
										</tbody>
									</table>
									<!--- <script id="volunteerWorkshopsListTemplate" type="text/x-kendo-tmpl">
										<tr>
											<td>#= first_name# #= last_name#</td>
											<td>
												#= site#<br>
												#= workshop_date# #= start_time# - #= end_time#
											</td>
											# if (confirmed) { #
												<td>Yes</td>
											# } else { #
												<td>No</td>
											# } #
											<!--- # if (acknowledged) { #
												<td>Yes</td>
											# } else { #
												<td>No</td>
											# } # --->
											<td>
												<div class="btn-group">
													<button class="btn btn-success" onclick="get('workshopVolunteer', #= workshop_volunteer_id#, 'edit', 'modal')">Edit</button>
													<button class="btn btn-success dropdown-toggle" data-toggle="dropdown"><span class="caret"></span></button>
													<ul class="dropdown-menu">
														<li><a onclick="confirmDel('workshopVolunteer', #= workshop_volunteer_id#)">Delete</a></li>
													</ul>
												</div>
											</td>
										</tr>
									</script> --->
								<cfelse>
									<div class="alert alert-info notFound">No assigned workshops</div>
								</cfif>
							</div>
						</div>
					</div>
				</div>
				<div class="modal-footer">
					<a href="?view=#returnView#&#passbackQueryString#" class="btn btn-success">Cancel</a>
					<button type="submit" name="saveButton" class="btn btn-success">Save</button>
				</div>
				<!--- <div class="modal-footer">
					<a href="?view=#returnView#&#passbackQueryString#" class="btn">Cancel</a>
					<!--- <a class="btn" href="?action=deleteVolunteer&view=#returnView#&Volunteer_ID=#volunteer.getVolunteer_ID()#">Delete</a> --->
					<cfinput type="submit" name="saveButton" class="btn" value="Save" /><!--- <a href="##" onclick="validate(); return false;">Validate</a> --->
				</div> --->
			</div>
		</cfform>
		
	</cfoutput>
	
	

<cfelse>

	<div class="alert alert-info notFound">Volunteer not found or insufficient permissions to perform this operation</div>

</cfif>

<!--- <div class="clearfix">
	<label class="required">Confirmed</label>
	<cfselect name="Confirmed" required="yes" message="Confirmed is required" class="span9">
		<option value="0"<cfif workshopVolunteer.getConfirmed() eq 0> selected</cfif>>No</option>
		<option value="1"<cfif workshopVolunteer.getConfirmed() eq 1> selected</cfif>>Yes</option>
	</cfselect>
</div>
<div class="clearfix">
	<label class="required">Acknowledged</label>
	<cfselect name="Acknowledged" required="yes" message="Acknowledged is required" class="span9">
		<option value="0"<cfif workshopVolunteer.getAcknowledged() eq 0> selected</cfif>>No</option>
		<option value="1"<cfif workshopVolunteer.getAcknowledged() eq 1> selected</cfif>>Yes</option>
	</cfselect>
</div>
<div class="kendo-control clearfix" style="display: #IIf(workshopVolunteer.getWorkshop_ID() gt 0, DE('hidden'), DE('block'))#">
	<label class="required">Workshop</label>
	<cfinput type="text" id="Workshop_ID" name="Workshop_ID" required="yes" message="Workshop is required" style="width: 74.4681%" />
</div> --->
<!--- <cfif workshopVolunteer.getWorkshop_ID() gt 0>
	<div class="text-control clearfix">
		<label>Workshop</label>
		#site.getSite()#<br />
		#workshop.getWorkshop_Date()# #workshop.getStart_Time()#-#workshop.getEnd_Time()#<br />
		#site.getAddress()#<br />
		#site.getCity()#, CA #site.getZip_Code()#
	</div>
</cfif>

	<div class="kendo-control clearfix" style="display: #IIf(workshopVolunteer.getVolunteer_ID() gt 0, DE('hidden'), DE('block'))#">
		<label class="required">Volunteer</label>
		<cfinput type="text" id="Volunteer_ID" name="Volunteer_ID" required="yes" message="Volunteer is required" style="width: 74.4681%" />
	</div>
 --->
